﻿using Raffles.OA.Data;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Raffles.OA.Service
{
    /// <summary>
    /// 用户服务
    /// </summary>
    public class UserService: BaseService
    {
        /// <summary>
        /// 获取所有销售角色的用户(用于下拉绑定)
        /// </summary>
        /// <returns></returns>
        public async Task<List<Dictionary<string, object>>> GetSalerSelectList(int? userId)
        { 
            return await _dbContext.Queryable<User>().Includes(x => x.Roles)
                                                    .Where(x => !x.DeletedMark && x.EnabledMark && !x.IsSystem && x.Roles.Any(y => y.Code == RoleTypeEnum.Sale.ToString()))
                                                    .WhereIF(userId != null, x => x.Id == userId)
                                                    .Select(x => new { Key = x.Id, Value = x.RealName })
                                                    .ToDictionaryListAsync();

        }

        /// <summary>
        /// 获取所有销售角色的用户(用于查询)
        /// </summary>
        /// <returns></returns>
        public async Task<List<User>> GetSalerList(int? userId)
        {
            return await _dbContext.Queryable<User>().Includes(x => x.Roles)
                                                    .Where(x => !x.DeletedMark && x.EnabledMark && !x.IsSystem && x.Roles.Any(y => y.Code == RoleTypeEnum.Sale.ToString()))
                                                    .WhereIF(userId != null, x => x.Id == userId)
                                                    .ToListAsync();

        }
    }
}
